package demoq;

import java.util.Comparator;
import java.util.PriorityQueue;

class Imp implements Comparator<Integer> {

    @Override
    public int compare(Integer o1, Integer o2) {
        return o2-o1;
    }
}
public class Test {
    //找到前k个最小元素

    public int[] findkMin(int[] array,int k) {
        int[] ret = new int[k];
        if(k == 0) {
            return ret;
        }
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(k,new Imp());
        for (int i = 0; i < k; i++) {
            maxHeap.offer(array[i]);
        }
        for (int i = k; i < array.length; i++) {
            //1.获取堆顶元素的值
            int top = maxHeap.peek();
            if (top > array[i]) {
                maxHeap.poll();
                maxHeap.offer(array[i]);
            }
        }
        for (int i = 0; i < k; i++) {
            ret[i] = maxHeap.poll();
        }
        return ret;
    }

    //
    public static void main1(String[] args) {
        TestHeap testHeap = new TestHeap();

        testHeap.push(12);
        testHeap.push(45);
        testHeap.push(14);
        testHeap.push(76);
        testHeap.push(54);
        testHeap.push(24);
        testHeap.push(75);
        testHeap.push(13);
        testHeap.push(78);
        testHeap.push(98);
        System.out.println();
    }
}
